2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex5_slaveMultipleSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
35 // MSP430FR57xx Demo - USCI_B0 I2C 4 Hardware I2C slaves
36 //
37 // Description: This demo connects two MSP430's via the I2C bus.
38 // This code configures the MSP430 USCI to be addressed as 4 independent I2C
39 // slaves. Each slave has its owm interrupt flag and data variable to store
40 // incoming data.
41 //
42 // Tested on MSP430FR5969
43 // /|\ /|\
44 // 10k 10k
45 // slave | | master
46 // ----------------- | | -----------------
47 // -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
48 // | | | | |
49 // -|XOUT | | | XOUT|-
50 // | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
51 // | | | |
52 // | | | |
53 //
54 //******************************************************************************
55 uint8_t RXData0=0;
56 uint8_t RXData1=0;
57 uint8_t RXData2=0;
58 uint8_t RXData3=0;
59 
60 void main(void)
61 {
62 
63  WDT_A_hold(WDT_A_BASE);
64 
65  // Configure Pins for I2C
66  //Set P1.6 and P1.7 as Secondary Module Function Input.
67  /*
68 
69  * Select Port 1
70  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
71  */
72  GPIO_setAsPeripheralModuleFunctionInputPin(
73  GPIO_PORT_P1,
74  GPIO_PIN6 + GPIO_PIN7,
75  GPIO_SECONDARY_MODULE_FUNCTION
76  );
77 
78  /*
79  * Disable the GPIO power-on default high-impedance mode to activate
80  * previously configured port settings
81  */
82  PMM_unlockLPM5();
83 
84  // eUSCI configuration
85  //SLAVE0 own address is 0x0A+ enable
86  EUSCI_B_I2C_initSlaveParam initSlave0Param = {0};
87  initSlave0Param.slaveAddress = 0x0A;
88  initSlave0Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET0;
89  initSlave0Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
90  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave0Param);
91 
92  //SLAVE1 own address is 0x0B+ enable
93  EUSCI_B_I2C_initSlaveParam initSlave1Param = {0};
94  initSlave1Param.slaveAddress = 0x0B;
95  initSlave1Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET1;
96  initSlave1Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
97  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave1Param);
98 
99  //SLAVE2 own address is 0x0C+ enable
100  EUSCI_B_I2C_initSlaveParam initSlave2Param = {0};
101  initSlave2Param.slaveAddress = 0x0C;
102  initSlave2Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET2;
103  initSlave2Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
104  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave2Param);
105 
106  //SLAVE3 own address is 0x0D+ enable
107  EUSCI_B_I2C_initSlaveParam initSlave3Param = {0};
108  initSlave3Param.slaveAddress = 0x0D;
109  initSlave3Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET3;
110  initSlave3Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
111  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave3Param);
112 
113  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
114 
115  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
116  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
117  EUSCI_B_I2C_RECEIVE_INTERRUPT1 +
118  EUSCI_B_I2C_RECEIVE_INTERRUPT2 +
119  EUSCI_B_I2C_RECEIVE_INTERRUPT3
120  );
121 
122  //receive interrupt enable
123  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
124  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
125  EUSCI_B_I2C_RECEIVE_INTERRUPT1 +
126  EUSCI_B_I2C_RECEIVE_INTERRUPT2 +
127  EUSCI_B_I2C_RECEIVE_INTERRUPT3
128  );
129 
130  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
131  __no_operation();
132 }
133 
134 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
135 #pragma vector=USCI_B0_VECTOR
136 __interrupt
137 #elif defined(__GNUC__)
138 __attribute__((interrupt(USCI_B0_VECTOR)))
139 #endif
140 void USCIB0_ISR(void)
141 {
142  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
143  {
144  case USCI_NONE: // No interrupts break;
145  break;
146  case USCI_I2C_UCALIFG: // Arbitration lost
147  break;
148  case USCI_I2C_UCNACKIFG: // NAK received (master only)
149  break;
150  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
151  break;
152  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
153  break;
154  case USCI_I2C_UCRXIFG3: // RXIFG3
155  RXData3 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
156  break;
157  case USCI_I2C_UCTXIFG3: // TXIFG3
158  break;
159  case USCI_I2C_UCRXIFG2: // RXIFG2
160  RXData2 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
161  break;
162  case USCI_I2C_UCTXIFG2: // TXIFG2
163  break;
164  case USCI_I2C_UCRXIFG1: // RXIFG1
165  RXData1 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
166  break;
167  case USCI_I2C_UCTXIFG1: // TXIFG1
168  break;
169  case USCI_I2C_UCRXIFG0: // RXIFG0
170  RXData0 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
171  break;
172  case USCI_I2C_UCTXIFG0: // TXIFG0
173  break;
174  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
175  break;
176  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
177  break;
178  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
179  break;
180  default:
181  break;
182  }
183 }
184 
__no_operation()